package city.spring.modules.oldman.controller;

import city.spring.modules.oldman.entity.OldmanFamilydependentEntity;
import city.spring.modules.oldman.service.OldmanFamilydependentService;
import city.spring.utils.MyBatisUtils;
import com.baomidou.mybatisplus.core.toolkit.support.SFunction;
import com.baomidou.mybatisplus.extension.api.ApiController;
import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.commons.lang.StringUtils;
import org.springframework.data.domain.Pageable;
import org.springframework.data.web.PageableDefault;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * Controller：老人家属信息
 *
 * @author 侯坤林
 * @date 2020-05-20 22:51:29
 */
@RestController
@RequestMapping("/oldman_familydependent")
public class OldmanFamilydependentController extends ApiController {
    private final OldmanFamilydependentService oldmanFamilydependentService;
    /**
     * 实体类可排序字段
     */
    private final List<SFunction<OldmanFamilydependentEntity, ?>> entityOrderFields;

    public OldmanFamilydependentController(OldmanFamilydependentService oldmanFamilydependentService) {
        this.oldmanFamilydependentService = oldmanFamilydependentService;
        entityOrderFields = new ArrayList<>();
        entityOrderFields.add(OldmanFamilydependentEntity::getId);
        entityOrderFields.add(OldmanFamilydependentEntity::getOldmanId);
        entityOrderFields.add(OldmanFamilydependentEntity::getName);
        entityOrderFields.add(OldmanFamilydependentEntity::getTelephone);
        entityOrderFields.add(OldmanFamilydependentEntity::getIdCard);
        entityOrderFields.add(OldmanFamilydependentEntity::getRelationshipOldman);
        entityOrderFields.add(OldmanFamilydependentEntity::getSex);
        entityOrderFields.add(OldmanFamilydependentEntity::getAddress);
        entityOrderFields.add(OldmanFamilydependentEntity::getIsGuardian);
    }

    /**
     * 获取全部的 <strong>老人家属信息</strong> 列表
     */
    @GetMapping("all")
    public Object listAll(@PageableDefault(sort = {"gmtCreate"}) Pageable pageable, OldmanFamilydependentEntity entity) {
        LambdaQueryChainWrapper<OldmanFamilydependentEntity> lambdaQuery = buildLambdaQuery(entity);
        MyBatisUtils.lambdaQueryAddOrder(lambdaQuery, pageable, entityOrderFields);
        return success(lambdaQuery.list());
    }

    /**
     * 分页获取 <strong>老人家属信息</strong> 列表
     *
     * @param pageable 分页参数信息
     */
    @GetMapping
    public Object list(@PageableDefault(sort = {"gmtCreate"}) Pageable pageable, OldmanFamilydependentEntity entity) {
        LambdaQueryChainWrapper<OldmanFamilydependentEntity> lambdaQuery = buildLambdaQuery(entity);
        MyBatisUtils.lambdaQueryAddOrder(lambdaQuery, pageable, entityOrderFields);
        Page<OldmanFamilydependentEntity> page = lambdaQuery.page(MyBatisUtils.toPage(pageable, false));
        return success(page);
    }

    /**
     * 获取一个 <strong>老人家属信息</strong>
     *
     * @param id 主键ID
     */
    @GetMapping("{id}")
    public Object info(@PathVariable String id) {
        return success(oldmanFamilydependentService.getById(id));
    }

    /**
     * 添加一个 <strong>老人家属信息</strong>
     *
     * @param entity 修改后的信息
     */
    @PostMapping
    public Object add(@RequestBody OldmanFamilydependentEntity entity) {
        oldmanFamilydependentService.saveOldmanFamilydependent(entity);

        HttpHeaders headers = new HttpHeaders();
        headers.add(HttpHeaders.LOCATION, String.valueOf(entity.getId()));
        return new ResponseEntity<>(headers, HttpStatus.CREATED);
    }

    /**
     * 修改一个 <strong>老人家属信息</strong>
     *
     * @param id     主键ID
     * @param entity 修改后的信息
     */
    @PutMapping("{id}")
    public Object update(@PathVariable String id, @RequestBody OldmanFamilydependentEntity entity) {
        entity.setId(id);
        oldmanFamilydependentService.updateOldmanFamilydependent(entity);
        return new ResponseEntity<>(HttpStatus.OK);
    }

    /**
     * 删除一个 <strong>老人家属信息</strong>
     *
     * @param id 主键ID
     */
    @DeleteMapping("{id}")
    public Object delete(@PathVariable String id) {
        oldmanFamilydependentService.deleteOldmanFamilydependent(id);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }

    /**
     * 删除多个 <strong>老人家属信息</strong>
     *
     * @param ids 主键ID列表
     */
    @DeleteMapping
    public Object deleteIds(@RequestBody List<String> ids) {
        oldmanFamilydependentService.deleteOldmanFamilydependent(ids);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }

    /**
     * 构建查询条件内容
     *
     * @param entity 实体对象
     * @return lambda query chain wrapper
     */
    private LambdaQueryChainWrapper<OldmanFamilydependentEntity> buildLambdaQuery(OldmanFamilydependentEntity entity) {
        LambdaQueryChainWrapper<OldmanFamilydependentEntity> lambdaQuery = oldmanFamilydependentService.lambdaQuery();
        lambdaQuery.eq(StringUtils.isNotBlank(entity.getId()), OldmanFamilydependentEntity::getId, entity.getId());
        lambdaQuery.eq(StringUtils.isNotBlank(entity.getOldmanId()), OldmanFamilydependentEntity::getOldmanId, entity.getOldmanId());
        lambdaQuery.like(StringUtils.isNotBlank(entity.getName()), OldmanFamilydependentEntity::getName, entity.getName());
        lambdaQuery.like(StringUtils.isNotBlank(entity.getTelephone()), OldmanFamilydependentEntity::getTelephone, entity
                .getTelephone());
        lambdaQuery.like(StringUtils.isNotBlank(entity.getIdCard()), OldmanFamilydependentEntity::getIdCard, entity.getIdCard());
        lambdaQuery.like(StringUtils.isNotBlank(entity.getRelationshipOldman()), OldmanFamilydependentEntity::getRelationshipOldman, entity
                .getRelationshipOldman());
        lambdaQuery.like(StringUtils.isNotBlank(entity.getSex()), OldmanFamilydependentEntity::getSex, entity.getSex());
        lambdaQuery.like(StringUtils.isNotBlank(entity.getAddress()), OldmanFamilydependentEntity::getAddress, entity.getAddress());
        lambdaQuery.eq(entity.getIsGuardian() != null, OldmanFamilydependentEntity::getIsGuardian, entity
                .getIsGuardian());
        return lambdaQuery;
    }

    /**
     * 根据老人id查找老人家属信息
     *
     * @param oldmanId
     * @return
     */
    @GetMapping("getFamilydependentByOldmanId/{oldmanId}")
    public Object getFamilydependentByOldmanId(@PathVariable String oldmanId) {
        return success(oldmanFamilydependentService.getFamilydependentByOldmanId(oldmanId));
    }

}
